package per.jdz.retryer.strategy;

import java.util.concurrent.TimeUnit;

/**
 * 超时重试策略
 */
public class TimeRetryerStrategy extends AbstractRetryerStrategy {
    private final int maxCount;

    public TimeRetryerStrategy(int count, long time, TimeUnit timeUnit) {
        super(count, time, timeUnit);
        this.maxCount = count;
    }

    @Override
    boolean exec() {
        while (this.count-- > 0) {
            try {
                this.timeUnit.sleep(this.time);
                return true;
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                throw new RuntimeException("Retry interrupted", e);
            }
        }
        return false;
    }

    @Override
    public void reset() {
        this.count = maxCount;
    }
}
